package com.hongxun.project.assets.exportdetailed;

import com.github.pagehelper.util.StringUtil;
import com.hongxun.framework.config.RuoYiConfig;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.project.assets.exportdetailed.dxf.WordUtil;
//import com.hongxun.project.assets.Tools.svgUtils;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.project.assets.iPullLine.domain.IPullLine;
import com.hongxun.project.assets.iPullLine.service.IIPullLineService;
import com.hongxun.project.assets.pDevice.service.IPDeviceService;
import com.hongxun.project.assets.pMaterial.service.IPMaterialService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.project.assets.pPullLine.domain.PPullLine;
import com.hongxun.project.assets.pPullLine.service.IPPullLineService;
import com.hongxun.project.assets.pTask.service.IPTaskService;
import com.hongxun.project.common.CommonController;
import com.hongxun.project.system.dept.domain.Dept;
import com.hongxun.project.system.dept.service.IDeptService;
import com.hongxun.project.system.user.domain.User;
import com.hongxun.project.system.user.service.IUserService;
//import com.itextpdf.text.pdf.BaseFont;
//import com.lowagie.text.DocumentException;
//import com.sun.net.httpserver.HttpHandler;
//import org.apache.batik.transcoder.Transcoder;
//import org.apache.batik.transcoder.TranscoderException;
//import org.apache.batik.transcoder.TranscoderInput;
//import org.apache.batik.transcoder.TranscoderOutput;
//import org.apache.batik.transcoder.image.JPEGTranscoder;
//import org.apache.fop.svg.PDFTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.xhtmlrenderer.pdf.ITextFontResolver;
//import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.*;

import static com.hongxun.common.utils.Arith.div;

/**
 * Created by john on 2019/4/29.
 */

@Controller
@RequestMapping("/assets/exportCad")
public class ExportDxf extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private IPPointService pPointService;
    @Autowired
    private IIPointService iPointService;
    @Autowired
    private IILineService iLineService;
    @Autowired
    private IPPullLineService pPullLineService;
    @Autowired
    private IIPullLineService iPullLineService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IPMaterialService pMaterialService;
    @Autowired
    private IPDeviceService pDeviceService;
    @Autowired
    private IPTaskService pTaskService;
    @Autowired
    private IBByqService bByqService;




    /*@PostMapping("/PDF")
    @ResponseBody
    public void expPdfFile(HttpServletRequest request, HttpServletResponse response) throws IOException, TranscoderException {

        svgToPdf(request,response);
        File svgFile = new File("D:\\profile\\download\\test_528\\东联村五组变_全部.svg");
        File pdfFile = new File("D:\\profile\\download\\test_528\\东联村五组变_全部.pdf");
        new svgUtils().convertSvgFile2Pdf(svgFile , pdfFile);
//        Transcoder transcoder = new PDFTranscoder();
//        TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("D:\\profile\\download\\test_528\\东联村五组变_全部.svg")));
//        TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("D:\\profile\\download\\test_528\\东联村五组变_全部.pdf")));
//        transcoder.transcode(transcoderInput, transcoderOutput);



    }
    public static void svgToPdf(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        //文件名
        String fileName = request.getParameter("pdf_file_name");
        //对HTML标签进行反转义，获取标签内容
        String svgHtml = request.getParameter("svgStrforPdf").replace("@#@","<")
                .replace("$#$",">");
        // TODO: 2019/5/28  将获取到的svg元素内容转存为svg格式文件，然后再通过svg文件转PDF文件方式进行转换
        String svgName = fileName;       //  svg文件名
        String svgPath = "D:\\profile\\download\\test_528\\"; //  svg文件保存路径
        String path = svgPath+svgName+".svg";
        File file = new File(path);          //  创建文件
        try {
            if(!file.exists()){                      //  当文件不存在时，进行创建
                if (file.isDirectory()){
                    file.mkdir();
                }
                file.createNewFile();

                //  读取流，将svg内容写入文件，关闭
                FileOutputStream fos = new FileOutputStream(file);
                PrintWriter pw = new PrintWriter(fos);
                pw.write(svgHtml);              //  写入到文件中
                pw.flush();
                pw.close();
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

*
     *  svg转PDF测试
     *
     * @param request
     * @param response


    @PostMapping("/svg2PDF")
    @ResponseBody
    public void svgToPdfTest(HttpServletRequest request, HttpServletResponse response) throws IOException, TranscoderException {
        response.setCharacterEncoding("utf-8");
        //文件名
        String fileName = request.getParameter("pdf_file_name");
        //对HTML标签进行反转义，获取标签内容
        String svgHtml = request.getParameter("svgStrforPdf").replace("@#@","<")
                .replace("$#$",">");

        // TODO: 2019/5/28  将获取到的svg元素内容转存为svg格式文件，然后再通过svg文件转PDF文件方式进行转换
        String svgName = fileName;       //  svg文件名
//        String svgPath = "D:\\profile\\download\\test_528\\"; //  svg文件保存路径
        String svgPath = "/templates/assets/bByq/"; //  svg文件保存路径
        String path = svgPath+"test.svg";
        File file = new File(path);          //  创建文件
        try {
//            if(!file.exists()){                      //  当文件不存在时，进行创建
//                if (file.isDirectory()){
//                    file.mkdir();
//                }
//                file.createNewFile();


                //  读取流，将svg内容写入文件，关闭
                FileOutputStream fos = new FileOutputStream(file);
                PrintWriter pw = new PrintWriter(fos);
                pw.write(svgHtml);              //  写入到文件中
                pw.flush();
                pw.close();
                fos.close();
//            }


            //
//            File files = new File(svgPath+path);
            //得到文件名
//            String filename = file.getName();
            //如果文件不存在会抛出FileNotFoundException异常，需要捕获后进行处理
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.setContentType("image/png");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + new CommonController().setFileDownloadHeader(request, fileName)+".png");
            OutputStream out = response.getOutputStream();
//            svg2PDF(svgHtml,out);
//            changeSvgToPdf("D:\\profile\\download\\test_528\\test.pdf",path);
//            changeSvgToJpg("D:\\profile\\download\\test_528\\test.png",path);     //  svg文件转png图片
            changeSvgToJpg("/templates/assets/bByq/test.png",path);     //  svg文件转png图片
            out.write(buffer);
            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

*
     * SVG转PDF
     * @param svgCode SVG代码
     * @param out 输出流
     * @throws TranscoderException
     * @throws IOException


    public void svg2PDF(String svgCode,OutputStream out) throws TranscoderException, IOException{
        Transcoder transcoder = new PDFTranscoder();
        TranscoderInput input = new TranscoderInput(new StringReader(svgCode));
        TranscoderOutput output = new TranscoderOutput(out);
        transcoder.transcode(input, output);
    }



*
     * 将svg转换成png格式图片
     *
     * @param pistrPngFile 图片路径
     * @param pistrSvgPath svg路径


    private void changeSvgToJpg(String pistrPngFile, String pistrSvgPath) throws MalformedURLException {
        Date date = new Date();
        long timeBegin = date.getTime();
        // svg文件路径
        String strSvgURI;
        OutputStream ostream = null;
        try {
            // 根据路径获得文件夹
            File fileSvg = new File(pistrSvgPath);
            // 构造一个表示此抽象路径名的 file:URI
            URI uri = fileSvg.toURI();
            // 根据此 URI 构造一个 URL
            URL url = uri.toURL();
            // 构造此 URL 的字符串表示形式
            strSvgURI = url.toString();
            // 定义一个通用的转码器的输入
            TranscoderInput input = new TranscoderInput(strSvgURI);
            // 定义图片路径
            String strPngPath = pistrPngFile;
            // 输入流
            ostream = new FileOutputStream(strPngPath);
            // 定义单路输出的转码器
            TranscoderOutput output = new TranscoderOutput(ostream);
            // 构造一个新的转码器，产生JPEG图像
            JPEGTranscoder transcoder = new JPEGTranscoder();
            // 设置一个转码过程，JPEGTranscoder.KEY_QUALITY设置输出png的画质精度，0-1之间
            transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1));
            // 转换svg文件为png
            transcoder.transcode(input, output);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TranscoderException e) {
            e.printStackTrace();
        } finally {
            try {
                ostream.flush();
                // 关闭输入流
                ostream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("转换图片成功");
            // 删除svg文件
            // fileSvg.delete();
        System.out.println(pistrSvgPath + "已删除！");
        date = new Date();
        long timeEnd = date.getTime();
        long time = timeEnd - timeBegin;
        System.out.println("耗时" + time + "毫秒");
    }


    private void changeSvgToPdf(String pistrPdfFile, String pistrSvgPath) throws MalformedURLException {
        Date date = new Date();
        long timeBegin = date.getTime();
        // svg文件路径
        String strSvgURI;
        OutputStream ostream = null;
        try {
            // 根据路径获得文件夹
            File fileSvg = new File(pistrSvgPath);
            // 构造一个表示此抽象路径名的 file:URI
            URI uri = fileSvg.toURI();
            // 根据此 URI 构造一个 URL
            URL url = uri.toURL();
            // 构造此 URL 的字符串表示形式
            strSvgURI = url.toString();
            // 定义一个通用的转码器的输入
            TranscoderInput input = new TranscoderInput(strSvgURI);
            // 定义图片路径
            String strPngPath = pistrPdfFile;
            // 输入流
            ostream = new FileOutputStream(strPngPath);
            // 定义单路输出的转码器
            TranscoderOutput output = new TranscoderOutput(ostream);
            // 构造一个新的转码器，产生JPEG图像
            PDFTranscoder transcoder = new PDFTranscoder();
            // 设置一个转码过程，JPEGTranscoder.KEY_QUALITY设置输出png的画质精度，0-1之间
//            transcoder.addTranscodingHint(PDFTranscoder.KEY_STROKE_TEXT, new Float(1));
            // 转换svg文件为png
            transcoder.transcode(input, output);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TranscoderException e) {
            e.printStackTrace();
        } finally {
            try {
                ostream.flush();
                // 关闭输入流
                ostream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("转换图片成功");
            // 删除svg文件
            // fileSvg.delete();
        System.out.println(pistrSvgPath + "已删除！");
        date = new Date();
        long timeEnd = date.getTime();
        long time = timeEnd - timeBegin;
        System.out.println("耗时" + time + "毫秒");
    }

*/





    @PostMapping("/DXF")
    @ResponseBody
    public void expDxfFile(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        try {
            String templateName = "dxf2007_test.ftl";	//模板名称
            String namss=request.getParameter("svgStr").replace("@#@","<")
                    .replace("$#$",">");//对HTML标签进行反转义

            // 生成DXF文件所需组码
            Map<String, Object> result = DxfUtil.svgToDxfData(namss);
            String fileOnlyName = System.currentTimeMillis() + ".dxf";

            String dxf_file_name = request.getParameter("dxf_file_name");//文件名
            if (StringUtil.isNotEmpty(dxf_file_name)) {
                fileOnlyName = dxf_file_name + ".dxf";
            }
            // 存放路径
            String filePath = RuoYiConfig.getDownloadPath()+"";
            //生成Excel
            String url = WordUtil.createWord(result, templateName, filePath, fileOnlyName);
            if (StringUtil.isNotEmpty(url)) {
                File file = new File(url);
                //得到文件名
                String filename = file.getName();
                //如果文件不存在会抛出FileNotFoundException异常，需要捕获后进行处理
                InputStream fis = new BufferedInputStream(new FileInputStream(url));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                //清空response
                response.reset();
                String downLoadName = new String(filename.getBytes("gbk"), "iso8859-1");
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename=" + downLoadName);
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } else {
                response.setContentType("text/html");
                try {
                    response.getWriter().print("<html><body><h1>生成dxf文档失败，请刷新后重试！</h1></body></html>");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            //log.error(e.getMessage(), e);
            e.printStackTrace();
            response.setContentType("text/html");
            try {
                response.getWriter().print("<html><body><h1>生成dxf文档失败，请刷新后重试！</h1></body></html>");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }





    public String getInfoByLine(String byq_id , String line_id){
        // TODO: 2019/5/8  1.线路各分段电压(10kv、380v、220v)下的线路长度、导线材料及型号、线路类型；

        ILine iLine = iLineService.selectILineById(line_id);
        
        Float line_length_all = 0f;    //  总线路长度
        StringBuffer line_info_all = new StringBuffer();

        // 10kv线路
        IPoint iPoint_10kv = new IPoint();
//        pPoint_10kv.setLineId(line_id);
        //  当默认为所有线路时
        List<IPoint> pointListSon = new ArrayList<>();
        List<IPoint> iPointList_10kv = new ArrayList<>();
        if (line_id.isEmpty()){
            BByq bByq = bByqService.selectBByqById(byq_id);
            ILine iLine1 = new ILine();
            iLine1.setByqId(byq_id);
            List<ILine> iLineList = iLineService.selectILineList(iLine1);
//            PPoint pPoint = new PPoint();
            for (ILine line : iLineList) {
                iPoint_10kv.setLineId(line.getId());
                pointListSon = iPointService.selectIPointList(iPoint_10kv);
                iPointList_10kv.addAll(pointListSon);
            }
        }else{
            iPoint_10kv.setLineId(line_id);
            pointListSon = iPointService.selectIPointList(iPoint_10kv);
            iPointList_10kv.addAll(pointListSon);
        }


//        List<PPoint> pPointList_10kv = pPointService.selectPPointList(pPoint_10kv);
        Float line_length_10kv = 0f;    //  10kv线路长度
        String line_mater_10kv = "";    //  10kv导线型号
        String vas_info = "";           //  线路信息
        /*if (pPointList_10kv != null){
            for (PPoint pPoint : pPointList_10kv) {
                if (pPoint.getSpan() != null && !pPoint.getType().equals("交叉跨越")){
                    line_length_all = line_length_all + pPoint.getSpan().floatValue();
                }
                if(pPoint.getVoltage()!=null){
                    if (pPoint.getVoltage().equals("10kv") && !pPoint.getType().equals("表箱")
                            && !pPoint.getType().equals("变压器") && !pPoint.getType().equals("交叉跨越")){
                        if(pPoint.getSpan() != null){   //  档距不为空且点位类型不为表箱时
                            line_length_10kv = pPoint.getSpan().floatValue()+line_length_10kv;
                            line_mater_10kv = pPoint.getMaterId();
                        }
                    }
                }
            }
        }else{
            error("该线路下无点位数据，请先核实信息！");
        }

        if (line_length_10kv != 0){
            vas_info = "10kv线路"+line_length_10kv/1000+"km，";
        }*/

        // 380v线路
        IPoint iPoint_380v = new IPoint();
        iPoint_380v.setLineId(line_id);
        iPoint_380v.setByqId(byq_id);
        List<IPoint> iPointList_380v = iPointService.selectIPointList(iPoint_380v);
        Float line_length_380v = 0f;    //  380v线路长度
        Float line_380v_1 = 0f;    //  380v线路长度
        Float line_380v_2 = 0f;    //  380v线路长度
        String line_mater_380v = "";    //  380v导线型号
        if(iPointList_380v != null){
            for (IPoint iPoint : iPointList_380v) {
                if (iPoint.getVoltage()!=null){
                    if (iPoint.getVoltage().equals("400v")){
                        if(iPoint.getType().equals("表箱") && iPoint.getInletLine() != null){
                            line_380v_1 = line_380v_1 + iPoint.getInletLine().floatValue();
                        }else if(iPoint.getSpan() != null){   //  档距不为空且点位类型不为表箱时（表箱的进户线长度不计算在总的导线长度，需要单独列出）
                            line_380v_2 = iPoint.getSpan().floatValue()+line_380v_2;
                            line_mater_380v = iPoint.getMaterId();
//                            line_length_all = line_length_all + pPoint.getSpan().floatValue();
                        }
                        line_length_380v = line_380v_1 + line_380v_2;
                    }
                }
            }
        }else{
            error("该线路下无点位数据，请先核实信息！");
        }
        if (line_length_380v != 0){
            vas_info = vas_info+"400v线路"+div(line_length_380v,1000,2)+"km，";
        }

        // 220v线路
        IPoint iPoint_220v = new IPoint();
        iPoint_220v.setLineId(line_id);
        iPoint_220v.setByqId(byq_id);
        List<IPoint> iPointList_220v = iPointService.selectIPointList(iPoint_220v);
        Float line_length_220v = 0f;    //  220v线路长度
        Float line_220v_1 = 0f;    //  220v线路长度
        Float line_220v_2 = 0f;    //  220v线路长度
        String line_mater_220v = "";    //  220v导线型号
        if (iPointList_220v != null){
            for (IPoint iPoint : iPointList_220v) {
                if (iPoint.getVoltage()!=null){
                    if (iPoint.getVoltage().equals("220v")){
                        if(iPoint.getType().equals("表箱") && iPoint.getInletLine() != null){
                            line_220v_1 = iPoint.getInletLine().floatValue()+line_220v_1;
                        }else if(iPoint.getSpan() != null){   //  档距不为空且点位类型不为表箱时（表箱的进户线长度不计算在总的导线长度，需要单独列出）
                            line_220v_2 = iPoint.getSpan().floatValue()+line_220v_2;
                            line_mater_220v = iPoint.getMaterId();
//                            line_length_all = line_length_all + pPoint.getSpan().floatValue();
                        }
                        line_length_220v = line_220v_1 + line_220v_2;
                    }
                }
            }
        }else{
            error("该线路下无点位数据，请先核实信息！");
        }
        if (line_length_220v != 0){
            vas_info = vas_info+"220v线路"+div(line_length_220v,1000,2)+"km，";
        }
        String ifos = removeDunhao(vas_info);
        line_length_all = line_length_380v + line_length_220v;


        if (line_length_all == 0)
            line_info_all.append("1.本线路总长"+(div(line_length_all,1000,2))+"km");
        else
            line_info_all.append("1.本线路总长"+(div(line_length_all,1000,2))+"km；其中"+ifos);
        line_info_all.append("。");

        // TODO: 2019/5/8  2.表箱进户线长度
        /*Float box_length = 0f;  //  进户线长度
        for (PPoint pPoint : pPointList_10kv) {
            if(pPoint.getType().equals("表箱") && pPoint.getInletLine()!=null){
                box_length = box_length+pPoint.getInletLine().floatValue();
            }
        }
        if (box_length == 0)
            line_info_all.append("2.本线路进户线总长"+(box_length/1000)+"km。");
        else
            line_info_all.append("2.本线路进户线总长"+(box_length/1000)+"km。");*/


        // TODO: 2019/5/8  3.杆塔基数、分属杆高(4\6\8\10\12\15\18)数量，杆塔类型；
        int pole_num = 0;   //  杆塔总数量
        int pole_num_1 = 0;   //  杆塔总数量
        int pole_num_2 = 0;   //  杆塔总数量
        int pole_one = 0;   //  同杆基数
        int pole_one_1 = 0;   //  同杆基数
        int pole_one_2 = 0;   //  同杆基数
        String poleHeightInfo = "";
        List<String> listPoleHeight = new ArrayList();  //  杆高集合
        for (IPoint iPoint : iPointList_10kv) {
            if(line_id !=""){
                if (iPoint.getType().equals("杆塔")){
                    pole_num_1 += 1;
                    if(iPoint.getIsPole().equals("是") && iPoint.getIsPole()!=null){
                        pole_one_1 += 1;
                    }
                    listPoleHeight.add(iPoint.getPoleHeight());
                }
                if (iPoint.getType().equals("同杆架设")){
                    pole_num_2 += 1;
                    if(iPoint.getIsPole().equals("是") && iPoint.getIsPole()!=null){
                        pole_one_2 += 1;
                    }
                    listPoleHeight.add(iPoint.getPoleHeight());
                }
                pole_num = pole_num_1 + pole_num_2;
                pole_one = pole_one_1 + pole_one_2;
            }else{
                if (iPoint.getType().equals("杆塔")){
                    pole_num_1 += 1;
                    if(iPoint.getIsPole().equals("是") && iPoint.getIsPole()!=null){
                        pole_one_1 += 1;
                    }
                    listPoleHeight.add(iPoint.getPoleHeight());
                }
//                if (iPoint.getType().equals("同杆架设")){
//                    pole_num_2 += 1;
//                    if(iPoint.getIsPole().equals("是") && iPoint.getIsPole()!=null){
//                        pole_one_2 += 1;
//                    }
//                    listPoleHeight.add(iPoint.getPoleHeight());
//                }
                pole_num = pole_num_1 + pole_num_2;
                pole_one = pole_one_1 + pole_one_2;
            }


        }
        List<String> list = removeDuplicate(listPoleHeight);    //  集合去重
        for (String s : list) {
            IPoint iPoints = new IPoint();
            iPoints.setByqId(byq_id);
            iPoints.setLineId(line_id);
            iPoints.setPoleHeight(s);
            int poleSonHeight = iPointService.selectCoutByPoleHeightOrCrossType(iPoints);   //  分属杆高的数量
            poleHeightInfo = poleHeightInfo+s+poleSonHeight+"基，";
        }
        String sasf = removeDunhao(poleHeightInfo);

        if(pole_num == 0){
            line_info_all.append("2.本线路共有杆塔"+pole_num+"基");
        }else
            line_info_all.append("2.本线路共有杆塔"+pole_num+"基(同杆"+pole_one+"基);其中"+sasf);
        line_info_all.append("。");

        // TODO: 2019/5/8  4.交叉跨越数量，分属类型及基数。
        /*int obstacleNum = 0;    //  交叉跨越总数量
        int obstacleSonNum = 0; //  分属类型的交叉跨越数量
        String obstacleInfo = "";
        List<String> listObstacle = new ArrayList<>();
        for (PPoint pPoint : pPointList_10kv) {
            if (pPoint.getType().equals("交叉跨越")){
                obstacleNum += 1;
                listObstacle.add(pPoint.getCrossType());
            }
        }
        List<String> stringList = removeDuplicate(listObstacle);
        for (String s : stringList) {
            PPoint pPoints = new PPoint();
            pPoints.setByqId(byq_id);
            pPoints.setLineId(line_id);
            pPoints.setCrossType(s);
            obstacleSonNum = pPointService.selectCoutByPoleHeightOrCrossType(pPoints);
            obstacleInfo = obstacleInfo+s+obstacleSonNum+"处，";
        }
        String cps = removeDunhao(obstacleInfo);
        if (obstacleNum == 0)
            line_info_all.append("4.本线路交叉跨越共"+obstacleNum+"处");
        else
            line_info_all.append("4.本线路交叉跨越共"+obstacleNum+"处；其中"+cps);
        line_info_all.append("。");*/


        return line_info_all.toString();
    }




    @PostMapping("getAllPointInfo")
    @ResponseBody
    public Map<String , Object> getAllPointInfo(String byq_id , String line_id){

        // TODO: 2019/5/10  默认展示该台区所有线路的单线图；当选择分支线时，展示当前分支线的单线图
        Map<String , Object> map = new HashMap<>();
        System.err.println("变压器ID="+byq_id+"    "+"线路ID="+line_id);
        BByq bByq = bByqService.selectBByqById(byq_id);
        ILine iLine = new ILine();
        iLine.setByqId(bByq.getId());
        List<ILine> iLineList = iLineService.selectILineList(iLine);

        // TODO: 2019/5/8  作图信息组合：点位信息、拉线信息、交叉跨越信息、线路信息描述、设计单位、工程名称、项目名称

        //  点位信息(杆塔、电缆井、表箱、交叉跨越)
        IPoint iPoint = new IPoint();
        List<IPoint> iPointList = new ArrayList<>();
        List<IPoint> iPointListSon = new ArrayList<>();

        int iost = 0;
        //    查询所有线路时标的
        if (line_id.isEmpty()){
                iPoint.setByqId(byq_id);
                iPointListSon = iPointService.selectIPointList(iPoint);
                for (IPoint point : iPointListSon) {

                    iost++;
                    point.setDxfState("0");

                    if(point.getType().equals("同杆架设")){


                        IPoint iPoint12 = new IPoint();
                        iPoint12.setPoleId(point.getPoleId());


                        //  不为同一上联杆的同杆点位
                        boolean tus = false;
                        List<IPoint> iPointList1 = iPointService.selectIPointList(iPoint12);

                        for (IPoint iPoint1 : iPointList1) {
                            for (IPoint iPoint2 : iPointList1) {
                                if(!iPoint1.getUpId().equals(iPoint2.getUpId())){
                                    tus = true;
                                }
                            }
                        }
                        if(iPointList1.size() > 1 && tus){
                            IPoint iPoint14 = iPointService.selectIPointById(point.getPoleId());
                            iPointList1.add(iPoint14);
                            ILine iLine12 = new ILine();
                            StringBuffer wsf = new StringBuffer();
                            String wr = "";
                            boolean tru = false;
                                for (IPoint iPoint1 : iPointList1) {
                                    if(iPoint1.getMaterId()==null){
                                        iPoint1.setMaterId("1");
                                        point.setMaterId("1");
                                        iLine12 = iLineService.selectILineById(iPoint1.getLineId());
                                        wsf.append(iLine12.getName());
                                        wsf.append(",");
                                        tru = true;
                                    }else{
                                        tru = false;
                                    }

                                }

                                for (IPoint iPoint1 : iPointList1) {
                                    for (IPoint iPoint2 : iPointListSon) {
                                        if(iPoint1.getId().equals(iPoint2.getId())){
                                            if(iPoint1.getType().equals("杆塔")){
                                                if(tru){
                                                    wr = wsf.substring(0,wsf.lastIndexOf(","));
                                                    iPoint2.setLineName(wr);
//                                        point.setLineName(wr);
                                                }else{
                                                    wr = "";
                                                }
                                            }
                                        }
                                    }

                                }

//                                wr = wsf.substring(0,wsf.lastIndexOf(","));
//                                point.setLineName(wr);
                        }

                        //  同一上联杆的同杆点位(变压器出来的第一根同杆)
                        IPoint iPoint13 = new IPoint();
                        iPoint13.setUpId(point.getUpId());
                        List<IPoint> iPointList13 = iPointService.selectIPointList(iPoint13);
                        if(iPointList13.size()>1){
                            ILine iLine13 = new ILine();
                            StringBuffer wsf = new StringBuffer();
                            String wr = "";
                            boolean tru = false;
                            for (IPoint iPoint1 : iPointList13) {
                                if(iPoint1.getMaterId()==null){
                                    point.setMaterId("1");
                                    iPoint1.setMaterId("1");
                                    iLine13 = iLineService.selectILineById(iPoint1.getLineId());
                                    wsf.append(iLine13.getName());
                                    wsf.append(",");
                                    tru = true;
                                }else{
                                    tru = false;
                                }

                            }
                            for (IPoint iPoint1 : iPointList13) {
                                for (IPoint iPoint2 : iPointListSon) {
                                    if(iPoint1.getId().equals(iPoint2.getId())){
                                        if(iPoint1.getType().equals("杆塔")){
                                            if(tru){
                                                wr = wsf.substring(0,wsf.lastIndexOf(","));
                                                iPoint2.setLineName(wr);
//                                        point.setLineName(wr);
                                            }else{
                                                wr = "";
                                            }
                                        }
                                    }
                                }

                            }



                            /*if(tru){
                                wr = wsf.substring(0,wsf.lastIndexOf(","));
                            }else{
                                wr = "";
                            }*/

//                            point.setLineName(wr);

                            /*IPoint iPoint2 = iPointService.selectIPointById(point.getPoleId());
                            ILine iLine1 = iLineService.selectILineById(point.getLineId());
                            ILine iLine2 = iLineService.selectILineById(iPoint2.getLineId());
                            if (iLine1 != null && iLine2 != null){
                                String xlw = iLine1.getName()+","+iLine2.getName();
                                point.setLineName(xlw);
                            }*/
                        }


                    }else{
                        if(!(point.getType().equals("杆塔") && point.getIsPole().equals("是"))){
                            if(iost % 3 == 0 ){
                                ILine iLine1 = iLineService.selectILineById(point.getLineId());
                                if(iLine1 !=null) point.setLineName(iLine1.getName());
                            }

                        }
                    }

                }
                iPointList.addAll(iPointListSon);

        }else{//    查询支线路时标的
            iPoint.setByqId(byq_id);
            iPoint.setLineId(line_id);
            iPointListSon = iPointService.selectIPointList(iPoint);
            for (IPoint point : iPointListSon) {
                point.setDxfState("1");
            }
            iPointList.addAll(iPointListSon);
        }


        List<IPoint> pointList = new ArrayList<>();
        for (IPoint point : iPointList) {
            if (!point.getType().equals("交叉跨越")){
                if(point.getType().equals("同杆架设")){
                    IPoint iPoint1 = iPointService.selectIPointById(point.getPoleId());
                    point.setPoleHeight(iPoint1.getPoleHeight());
                    pointList.add(point);
                }else
                    pointList.add(point);

            }
        }
        //  拉线信息
        List<IPullLine> pullLineList = iPullLineService.selectIPullLineList(new IPullLine());
        List<IPullLine> pullLineListInfo = new ArrayList<>();
        for (IPoint point : iPointList) {
            for (IPullLine iPullLine : pullLineList) {
                if(point.getPullNum() != null && point.getPullNum() >0){
                    if (point.getId().equals(iPullLine.getPId())){
                        pullLineListInfo.add(iPullLine);
                    }
                }
            }
        }
        //  交叉跨越信息
        List<IPoint> crossList = new ArrayList<>();
        for (IPoint point : iPointList) {
            if (point.getType().equals("交叉跨越")){
                crossList.add(point);
            }
        }
        //  线路信息
        String lineInfo = getInfoByLine(byq_id,line_id);
        System.err.println("lineInfo=="+lineInfo);
        //  设计单位
        User user = userService.selectUserById(getUserId());
        Dept dept = deptService.selectDeptById(user.getDeptId());
        String complanName = dept.getDeptName();

        //  工程名称(项目名称)
//        String projectName = iLineService.selectILineById(id).getName();
        String projectName = "";
//        bByqService.selectBByqById()

        map.put("pointList",pointList);
        map.put("laxianList",pullLineListInfo);
        map.put("crossCostList",crossList);
        map.put("textgcmc",projectName);
        map.put("textsjdw",complanName);
        map.put("projectName",projectName);
        map.put("pointInfo",lineInfo);

        return map;
    }





    private static List<String> removeDuplicate(List<String> list) {
        LinkedHashSet<String> set = new LinkedHashSet<String>(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }





    public static String removeDunhao(String str){
        if(str!=null && !str.equals("")){
            if(str.lastIndexOf("，") == str.length()-1){
                str = str.substring(0,str.length()-1);
            }
        }
        return str;
    }




}
